package com.mvc4g.example.mockup.client.view.templates;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.client.Controller;
import com.mvc4g.client.ViewInterface;
import com.mvc4g.example.mockup.client.HistoryChangeHandler;

public class RootTemplate implements ViewInterface {

	private SimplePanel body = new SimplePanel();
	
	public RootTemplate(){
		
		RootPanel.get().add(body);
	}
	
	@Override
	public void setController(Controller controller) {
		HistoryChangeHandler historyHandler = new HistoryChangeHandler(controller);
		History.addValueChangeHandler(historyHandler);
	}
	
	public void changePage(Widget newView, String historyToken) {
		body.clear();
		newView.setVisible(true);		
		body.add(newView);
		
		if(historyToken != null){
			History.newItem(historyToken, false);
		}
	}

}
